package com.hly.versioncontroladmin.controller;

import com.hly.versioncontroladmin.domain.Bo.VcCategoryBo;
import com.hly.versioncontroladmin.domain.VcApplication;
import com.hly.versioncontroladmin.domain.VcCategory;
import com.hly.versioncontroladmin.result.Code;
import com.hly.versioncontroladmin.result.PageQuery;
import com.hly.versioncontroladmin.result.Result;
import com.hly.versioncontroladmin.service.VcApplicationService;
import com.hly.versioncontroladmin.service.VcCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/category")
@RequiredArgsConstructor
public class VcCategoryController {

    private final VcCategoryService categoryService;
    private final VcApplicationService applicationService;

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") String id){
        return Result.ok(categoryService.getById(id));
    }

    @GetMapping("/list")
    public Result list(PageQuery<VcCategory> pageQuery, VcCategoryBo categoryBo){
        return Result.ok(categoryService.listQuery(pageQuery, categoryBo));
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody VcCategory type){
        return Result.ok(categoryService.saveOrUpdate(type));
    }

    @PostMapping("/del/{ids}")
    public Result del(@PathVariable("ids") String ids){
        List<String> strings = Arrays.asList(ids.split(","));
        if (applicationService.lambdaQuery()
                .in(VcApplication::getCategoryId,
                strings).list().size() > 0){
            Code.BUSINESS_ERR("该分类存在应用不可删除！请先删除应用");
        }
        return Result.ok(categoryService.removeBatchByIds(strings));
    }
}
